﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace YourFileWatcher.Models
{

    /// <summary>
    /// 登录的用户信息
    /// </summary>
    [Serializable]
    public class UserInfo
    {

        #region 属性成员

        /// <summary>
        /// 用户名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 登录账号
        /// </summary>
        public string LoginId { get; set; }

        /// <summary>
        /// 登录密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 登陆用户类型
        /// </summary>
        public UserType UserType { get; set; }

        #endregion



        /// <summary>
        /// 从指定的文件加载用户信息
        /// </summary>
        /// <param name="pFilePath">文件路径</param>
        public static UserInfo FromFile(string pFilePath)
        {
            try
            {
                return Ser.SerializeFile.DeserializeBinary<UserInfo>(pFilePath);
            }
            catch (Exception)
            {
                return new UserInfo();
            }
        }

        /// <summary>
        /// 将用户信息序列化保存到指定的目录
        /// </summary>
        /// <param name="pDirectoryPath">要保存到的目录</param>
        /// <returns>是否成功</returns>
        public bool SerilizeFile(string pDirectoryPath)
        {
            bool result = false;

            try
            {
                string filePath = Path.Combine(pDirectoryPath, LoginId + ".db");
                Ser.SerializeFile.SerializeBinary(filePath, this);
                result = true;
            }
            catch (Exception)
            {

            }
            return result;
        }

        /// <summary>
        /// 从指定的保存目录，删除用户信息文件
        /// </summary>
        /// <param name="pDirectoryPath">保存的目录</param>
        internal void DeleteSerializeFile(string pDirectoryPath)
        {
            string filePath = Path.Combine(pDirectoryPath, LoginId + ".db");

            if (!File.Exists(filePath)) throw new FileNotFoundException();

            File.Delete(filePath);
        }
    }
}
